<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" append-to-body center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm">
      <el-form-item label="家庭组名称" prop="familyGroupName">
        <el-input v-model="dataForm.familyGroupName" placeholder="家庭组名称"></el-input>
      </el-form-item>
      <el-table :data="dataList" border header-cell-class-name="miui-header-cell-style" v-if="dataForm.id != 0">
        <el-table-column prop="customerName" label="会员名称"></el-table-column>
        <el-table-column prop="integral" label="积分"></el-table-column>
      </el-table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="ischeck">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { getObj } from "@/api/admin/dept"
  import { getFamilyById, addObj, putObj } from '@/api/eims/familygroup'

  export default {
    data() {
      return {
        visible: false,
        ischeck: false,
        dataForm: {
          id: 0,
          familyGroupName: '',
          unitId: '',
          tanentId: '',
          createUser: '',
          createTime: '',
          updateUser: '',
          updateName: ''
        },
        dataList: [],
        dataRule: {
          familyGroupName: [{required: true, message: '家庭组名称不能为空', trigger: 'blur'}]
        }
      }
    },
    computed: {
      ...mapGetters(['userInfo'])
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.dataForm.deptId = this.userInfo.lessDeptId
        getObj(this.userInfo.lessDeptId).then(response => {
          if(response.data.code == '0'){
            this.dataForm.unitId = response.data.data.unitId
            this.dataForm.tenantId = response.data.data.tenantId
          }else{
            this.$message.error(response.data.msg)
          }
        })
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getFamilyById(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
                if (response.data.data.links) {
                  this.dataList = response.data.data.links
                }
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              addObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }
        })
      }
    }
  }
</script>
